<div class="filter">
    <div class="f1 item">
        <select>
            <option value="0">系统默认盘口</option>
            <option value="1">代理盘口</option>
            <option value="2">用户盘口</option>
        </select>
    </div>
    
    <div class="f2 item">
        <input class="am-form-field" placeholder="请输入账号名">
    </div>
    <div class="f12 item">
        <button type="button" class="am-btn am-btn-primary">确定</button>
    </div>
</div>

<div id="games">
</div>

<div class="red" style="line-height: 36px; font-size: 14px; font-weight: bold;" id="clue5">
    【注意】您现在修改的是代理用户 <span class="blue">test</span> 的代理盘口！
</div>
<div class="red" style="line-height: 36px; font-size: 14px; font-weight: bold;" id="clue6">
    【注意】您现在修改的是用户 <span class="blue">test</span> 的盘口！
</div>

<table style="width: 100%"><tr>
    <td style="width: 120px; vertical-align: top;" id="type-menus">
        <div class="mt">玩法分类</div>
        <div class="list">
        </div>
    </td>

    <td style="width: 700px; vertical-align: top;">
        <table id="lottery">
            <thead>
                <tr>
                    <th colspan="1">玩法</th>
                    <th width="120px">赔率</th>
                    <th>退水</th>
                    <th>单注最低</th>
                    <th>单注最高</th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </td>

    <td style="padding: 10px 0px 0px 10px; vertical-align: top;">
        <div class="title5">单期限额</div>
        <div class="content5">
            <input type="number" class="am-form-field game-max" placeholder="最大限额">
            <button type="button" class="btn-issue-limit">保存</button>
            <div class="clue">说明：不填或者填写0表示不限制。</div>
        </div>

        <div class="title5">单注限额</div>
        <div class="content5">
            <input type="number" class="am-form-field quick-min" placeholder="最小限额">
            <input type="number" class="am-form-field quick-max" placeholder="最大限额">
            <button type="button" class="btn-quick">快速设置</button>
            <div class="clue">
                注：代理盘口无法设置单注限额，仅限设置代理赔率；用户盘口无法设置赔率，仅限设置单注/单期限额。
            </div>
        </div>
    </td>
</table>
<div id="game-page"></div>

<div class="button_line">
    <button class="am-btn am-btn-primary btn-save admin_button">保存</button>
    <button class="am-btn am-btn-primary btn-reset-game admin_button" style="display:none">重置本游戏赔率</button>
    <button class="am-btn am-btn-primary btn-reset-all admin_button" style="display:none">重置所有赔率</button>
</div>

<style type="text/css">
    #lottery {
        width: 100%;
        border-right: 1px solid #ccc;
        border-bottom: 1px solid #ccc;
        margin-top: 10px;
    }
    #lottery td, #lottery th {
        border-left: 1px solid #ccc;
        border-top: 1px solid #ccc;
        text-align: center;
        vertical-align: middle;
    }
    #lottery td.title {
        width: 100px;
    }
    #lottery th {
        line-height: 28px;
        background-color: #eee;
    }
    #lottery input {
        text-align: center;
    }

    #games a {
        display: inline-block;
        margin: 5px;
        text-align: center;
        color: #6e706d;
        font-size: 14px;
        background-color: #eee;
        width: 100px;
        line-height: 24px;
        border-radius: 5px;
    }
    #games a.selected {
        background-color: #65f2fc;
        color: #000;
    }

    div.title5 {
        font-size: 14px;
        font-weight: bold;
        color: #000;
        line-height: 30px;
    }
    div.content5 {
        padding: 10px;
        min-width: 120px;
    }
    div.content5 .clue { color: red; line-height: 16px; }
    .content5 button {
        background-color: #13a7a9;
        border: 0px;
        border-radius: 3px;
        line-height: 26px;
        padding: 0px 8px 0px 8px;
        min-width: 80px;
        color: #fff;
        margin-top: 10px;
    }

    #type-menus .mt {
        text-align: center;
        font-size: 14px;
        color: #000;
        border-bottom:1px solid #ccc;
        line-height: 40px;
        margin-bottom: 8px;
    }
    #type-menus a {
        display: block;
        text-align: center;
        width: 100px;
        line-height: 28px;
        background-color: #ccc;
        border-radius: 3px;
        font-size: 13px;
        margin: auto;
        margin-bottom: 10px;
        color: #000;
    }
    #type-menus a.active {
        background: #13a7a9;
        color: #fff;
    }
</style>

<script src="../public/all/js/pages/lottery_info.js"></script>
<script>
    var targetUser = null;
    var targetOper = '';
    if (context.center.lotteryOper == 'edit') {
        targetOper = "referee";
        targetUser = context.center.user;
    }
    else if (context.center.lotteryOper == 'person') {
        targetOper = 'person';
        targetUser = context.center.user;
    }
    if (targetUser) {
        $(".filter .f2 input").val(targetUser.mobile);

        if (targetOper == 'referee') {
            $(".filter .f1 select").val(1);
            $("#clue5").show();
            $("#clue5 .blue").html(targetUser.mobile);
            $("#clue6").hide();
        }
        else {
            $(".filter .f1 select").val(2);
            $("#clue6").show();
            $("#clue6 .blue").html(targetUser.mobile);
            $("#clue5").hide();
        }
        
        $(".btn-reset-game").show();
        $(".btn-reset-all").show();
    }
    else {
        $(".filter .f1 select").val(0);
        $(".filter .f2 input").val("");
        $("#clue5").hide();
        $("#clue6").hide();
        $(".btn-reset-game").hide();
        $(".btn-reset-all").hide();
    }
    $(".filter .f12 button").click(function() {
        var v = $(".filter .f1 select").val();
        if (v == 0) {
            targetUser = null;
            targetOper = null;
            context.center.lotteryOper = "";
            $(".filter .f2 input").val("");
            $("#clue5").hide();
            $("#clue6").hide();
            $(".btn-reset-game").hide();
            $(".btn-reset-all").hide();
            updateGameLottery($("#games a.selected").attr("value"));
        }
        else if (v == 1) {
            targetOper = "referee";
            var v2 = $(".filter .f2 input").val().trim();
            if (!v2) {
                alert2("请输入代理账号名", true);
                return;
            }

            // 查找用户信息
            startLoading();
            CMD.get_user_list({
                uid: v2,
            }, 1, 1, function(r) {
                stopLoading();
                if (Util.isEmpty(r.list)) alert2("用户不存在！", true);
                else if (r.list[0].grade != 49) {
                    alert2("此用户不是代理，无法设置代理赔率！", true);
                }
                else if (r.list[0].refereeType != 1) {
                    alert2("此代理的代理模式为“分红”，无法设置代理赔率！", true);
                }
                else {
                    targetUser = r.list[0];
                    $("#clue5 .blue").html(targetUser.mobile);
                    $("#clue5").show();
                    $("#clue6").hide();
                    $(".btn-reset-game").show();
                    $(".btn-reset-all").show();
                    updateGameLottery($("#games a.selected").attr("value"));
                }
            });
        }
        else if (v == 2) {
            targetOper = "person";
            var v2 = $(".filter .f2 input").val().trim();
            if (!v2) {
                alert2("请输入账号名", true);
                return;
            }

            // 查找用户信息
            startLoading();
            CMD.get_user_list({
                uid: v2,
            }, 1, 1, function(r) {
                stopLoading();
                if (Util.isEmpty(r.list)) alert2("用户不存在！", true);
                else {
                    targetUser = r.list[0];
                    $("#clue6 .blue").html(targetUser.mobile);
                    $("#clue6").show();
                    $("#clue5").hide();
                    $(".btn-reset-game").show();
                    $(".btn-reset-all").show();
                    updateGameLottery($("#games a.selected").attr("value"));
                }
            });
        }
    });

    (function() {
        var _loadGame = function() {
            var g = $("#games a.selected").attr("value");
            switch_page("admin/lottery/" + g + "_lottery", $("#game-page"))
        }

        var div = $("#games"); div.empty();
        var games = get_game_list(window.bocai);
        var a = '<a href="#" value="__v__">__n__</a>';
        for (var i in games) {
            var l = a.replace(/__v__/, games[i]);
            l = l.replace(/__n__/, get_game_display_name(games[i]));
            div.append(l);
        }
        $("#games a:first").addClass("selected");
        $("#games a").click(function(e) {
            $("#games a").removeClass("selected");
            $(e.currentTarget).addClass("selected");
            _loadGame();
        })
        _loadGame();
    })();

    function fillTypeMenus() {
        var arr = $("#lottery td.classify");
        var div = $("#type-menus .list"); div.empty();
        for (var i = 0; i < arr.length; i++) {
            div.append('<a href="#">' + $(arr[i]).html() + '</a>');
        }
        var selectType = $(arr[0]).html();
        var f = function(selectType) {
            var arr = $("#type-menus a");
            arr.removeClass("active");
            for (var i in arr) {
                if ($(arr[i]).html() == selectType) {
                    $(arr[i]).addClass("active");
                    break;
                }
            }

            // 非本类的行都隐藏掉
            var arr = $("#lottery tbody tr");
            arr.hide();
            for (var i = 0; i < arr.length; i++) {
                var tr = $(arr[i]);
                if (tr.find(".classify").html() != selectType) continue;

                // 显示出来
                var rows = parseInt(tr.find(".classify").attr("rowspan"));
                for (var j = i; j < i + rows; j++) {
                    $(arr[j]).show();
                }
                break;
            }
        }
        f(selectType);
        $("#type-menus a").click(function(e) {
            f($(e.currentTarget).html());
        });
    }

    function updateGameLottery(g, system) {
        fillTypeMenus();

        startLoading();
        var f = function(r) {
            stopLoading();
            var m = {};
            for (var i in r) {
                m[r[i].target] = r[i];
            }
            _fillLottery(g, m);
        }
        if (system) {
            if (Util.isEmpty(r)) {
                CMD.query_lottery_rate(g, "", function(r2) {
                    f(r2);
                });
            }
            return;
        }

        var owner = '';
        if (targetUser) {
            if (targetOper != '') owner = targetUser.id;
        }
        CMD.query_lottery_rate(g, owner, function(r) {
            if (Util.isEmpty(r)) {
                CMD.query_lottery_rate(g, "", function(r2) {
                    f(r2);
                });
            }
            f(r);
        });
    }

    function _fillLottery(g, list) {
        if (list.targets) {
            $(".game-max").val(list.targets.max/100);
        }
        
        var rule = Util.getGameRule(g);
        var map = rule.map;
        for (var k in list) {
            if (isEmpty(map[k])) {
                continue;
            }
            if (list[k].value)
                map[k].rate = list[k].value;
            if (list[k].back)
                map[k].back = list[k].back;
            map[k].min = list[k].min;
            map[k].max = list[k].max;
        }

        var trs = $("#lottery tbody tr");
        for (var i = 0; i < trs.length; i++) {
            var k = $(trs[i]).attr("key");
            if (!k) continue;
            if (!map[k]) {
                console.log("不存在类型", k);
                continue;
            }

            $(trs[i]).find(".rate input").val(map[k].rate);

            if (!map[k].back) $(trs[i]).find(".back input").val(0);
            else $(trs[i]).find(".back input").val(100 * map[k].back);

            if (targetOper == 'referee') {
                // TODO
            }
            else {
                if (!map[k].min) $(trs[i]).find(".min input").val(0);
                else $(trs[i]).find(".min input").val(map[k].min / 100);

                if (!map[k].max) $(trs[i]).find(".max input").val(0);
                else $(trs[i]).find(".max input").val(map[k].max / 100);
            }
        }
        if (targetOper == 'referee') {
            $("td.min input").val("");
            $("td.min input").attr("disabled", "disabled");
            $("td.max input").val("");
            $("td.max input").attr("disabled", "disabled");
        }
        else {
            $("td.min input").removeAttr("disabled");
            $("td.max input").removeAttr("disabled");
        }
    }

    $(".button_line button.btn-save").click(function() {
        var m1 = {}, m2 = {}, m3 = {};
        var trs = $("#lottery tbody tr");
        for (var i = 0; i < trs.length; i++) {
            var k = $(trs[i]).attr("key");
            if (!k) continue;

            m1[k] = $(trs[i]).find(".rate input").val() || 1;
            m2[k] = $(trs[i]).find(".back input").val() || 0;
            m2[k] = parseFloat(m2[k]) / 100;
        
            var v1 = $(trs[i]).find(".min input").val() || 0;
            v1 = v1 * 100;
            var v2 = $(trs[i]).find(".max input").val() || 0;
            v2 = v2 * 100;
            m3[k] = v1 + "," + v2;
        }
        var g = $("#games a.selected").attr("value");
        if (!targetUser) {
            // 标准赔率
            console.log("赔率", m1);
            startLoading();
            CMD.lottery_rate_save(g, m1, function() {
                stopLoading();
                alert2("保存成功。");
            });

            console.log("返水", m2);
            CMD.lottery_back_save(g, m2, function() {
            });

            console.log("限额", m3);
            CMD.lottery_rate_ranges(g, m3, function() {
            });
        }
        else if (targetOper == "person") {
            // 单独赔率
            startLoading();
            /*
            CMD.lottery_rate_usaves(targetUser.id, g, m1, function() {
                stopLoading();
                alert2("保存成功。");
            });

            CMD.lottery_back_usaves(targetUser.id, g, m2, function() {
            });
            */
            CMD.lottery_rate_uranges(targetUser.id, g, m3, function() {
                stopLoading();
            });
        }    
        else if (targetOper == "referee") {
            // 代理赔率
            console.log("代理赔率", m1);
            startLoading();
            m1.owner = targetUser.id;
            CMD.lottery_rate_rsaves(g, m1, function() {
                stopLoading();
                alert2("保存成功。");
            });

            console.log("代理返水", m2);
            m2.owner = targetUser.id;
            CMD.lottery_rate_rbacks(g, m2, function() {
            });
        }        
    });

    $(".btn-quick").click(function() {
        if (targetOper == "referee") {
            alert2("代理不能单独设置限额！", true);
            return;
        }

        var v1 = $(".quick-min").val(), v2 = $(".quick-max").val();
        var arr = $("#lottery tbody tr");
        for (var i = 0; i < arr.length; i++) {
            if ($(arr[i]).css("display") == "none") continue;
            $(arr[i]).find(".min input").val(v1);
            $(arr[i]).find(".max input").val(v2);
        }
    });
    $(".btn-issue-limit").click(function() {
        if (targetOper == "referee") {
            alert2("代理不能单独设置限额！", true);
            return;
        }

        var v = $(".game-max").val().trim();
        if (v == '') {
            alert2("请填入最大单期限额的金额（单位：元）！", true);
            return;
        }

        v = parseInt(v);
        if (v < 0) {
            alert2("最大限额应该大于等于0！", true);
            return;
        }

        var g = $("#games a.selected").attr("value");
        if (targetOper == "person") {
            CMD.lottery_rate_uranges(targetUser.id, g, {
                targets: "0," + (v * 100),
            }, function() {
                alert2("修改完成！");
            });
        }
        else {
            CMD.lottery_rate_ranges(g, {
                targets: "0," + (v * 100),
            }, function() {
                alert2("修改完成！");
            });
        }
    });

    $(".btn-reset-game").click(function() {
        var g = $("#games a.selected").attr("value");
        if (targetOper == 'referee') {
            if (!confirm("确定重置" + get_game_display_name(g) + "的代理赔率？")) return;
            _resetGame(g, true);
        }
        else if (targetOper == 'person') {
            if (!confirm("确定重置" + get_game_display_name(g) + "的个人赔率？")) return;
        }
        startLoading();
        CMD.lottery_rate_reset(g, targetUser.id, function() {
            stopLoading();
            updateGameLottery($("#games a.selected").attr("value"));
        });
    });
    $(".btn-reset-all").click(function() {
        if (targetOper == 'referee') {
            if (!confirm("确定重置所有游戏的代理赔率？本过程将耗费比较长的时间，请耐心等待。")) return;
        }
        else if (targetOper == 'person') {
            if (!confirm("确定重置所有游戏的个人赔率？本过程将耗费比较长的时间，请耐心等待。")) return;
        }
        CMD.lottery_rate_reset(null, targetUser.id, function() {
            stopLoading();
            updateGameLottery($("#games a.selected").attr("value"));
        });
    });
</script>
